
// import haxe.ds.Array;

/**
 * numberernal Class
 */
export class BoundarySelector {
	n: number;
	indices: Array<number>;
	tmpIndices: Array<number>;

	constructor(n: number) {
		this.n = n;
		this.indices = new Array<number>(n);
		this.tmpIndices = new Array<number>(n);
		for (let i = 0; i < n; ++i) {
			this.indices[i] = i;
		}
	}

	public getIndex(i: number): number {
		return this.indices[i];
	}

	public select(index: number): void {
		var i: number = 0;
		while (this.indices[i] != index) {
			i++;
		}
		while (i > 0) {
			var tmp: number = this.indices[i];
			this.indices[i] = this.indices[i - 1];
			this.indices[i - 1] = tmp;
			i--;
		}
		// validate();
	}

	/**
	 * Makes first n elements the permutation of {0, 1, ... , n-1}
	 */
	public setSize(size: number): void {
		var numSmaller: number = 0;
		var numGreater: number = 0;
		for (let i = 0; i < this.n; ++i) {
			var idx: number = this.indices[i];
			if (idx < size) {
				this.tmpIndices[numSmaller] = idx;
				numSmaller++;
			} else {
				this.tmpIndices[size + numGreater] = idx;
				numGreater++;
			}
		}
		var tmp: Array<number> = this.indices;
		this.indices = this.tmpIndices;
		this.tmpIndices = tmp;
		// validate();
	}

	/*
	public validate():void {
		var po = new Array<boolean>(n);
		for (i in 0...n) {
			if (po[indices[i]]) {
				throw M.error("???"));
			} else {
				po[indices[i]] = true;
			}
		}
		for (i in 0...9) {
			if (!po[i]) {
				throw M.error("???"));
			}
		}
	}
	*/

}
